<!doctype html>
<!--
Copyright 2018 The Immersive Web Community Group

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
  <head>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'>
    <meta name='mobile-web-app-capable' content='yes'>
    <meta name='apple-mobile-web-app-capable' content='yes'>
    <link rel='icon' type='image/png' sizes='32x32' href='../favicon-32x32.png'>
    <link rel='icon' type='image/png' sizes='96x96' href='../favicon-96x96.png'>
    <link rel='stylesheet' href='../css/common.css'>

    <title>WebGL 2.0 Multisample</title>

    <script src='../js/third-party/dat.gui.min.js'></script>
  </head>
  <body>
    <header>
      <details id='details-panel' open>
        <summary>WebGL 2.0 Multisample</summary>
        <p>
          Tests that applications that use WebGL 2.0 Multisampled Renderbuffers
          can succesfully blit the result to an XRWebGLLayer.
          <a class="back" href="./">Back</a>
          <br/>
          <input type="radio" id="useRenderbuffer" name="xrTarget" value="useRenderbuffer" checked>
          <label for="useRenderbuffer">use renderbuffers</label><br>
          <input type="radio" id="useTexture" value="useTexture" name="xrTarget">
          <label for="useTexture">use textures</label><br>
          <input type="radio" id="useMultisampleTexture" value="useMSTexture" name="xrTarget" disabled>
          <label for="useTexture">use textures with multisampling</label><br>
          <input type="radio" id="useFoveatedTexture" value="useFoveatedTexture" name="xrTarget" disabled>
          <label for="useFoveatedTexture">use foveated textures</label><br>
        </p>
      </details>
    </header>
    <script type="module">
      import {WebXRSampleApp} from '../js/webxr-sample-app.js';
      import {createWebGLContext} from '../js/render/core/renderer.js';
      import {Gltf2Node} from '../js/render/nodes/gltf2.js';
      import {SkyboxNode} from '../js/render/nodes/skybox.js';
      import {WebXRView} from '../js/render/scenes/scene.js';
      import {QueryArgs} from '../js/util/query-args.js';

      // If requested, use the polyfill to provide support for mobile devices
      // and devices which only support WebVR.
      import WebXRPolyfill from '../js/third-party/webxr-polyfill/build/webxr-polyfill.module.js';
      if (QueryArgs.getBool('usePolyfill', true)) {
        let polyfill = new WebXRPolyfill();
      }

      let xrTargets = document.getElementsByName('xrTarget');

      // WebXR sample app setup
      class CustomWebXRSampleApp extends WebXRSampleApp {
        getXRTarget() {
          for(let radio of xrTargets) { 
              if(radio.checked) return radio.value; 
          } 
        }
        
        onCreateGL() {
          const gl = createWebGLContext({
            webgl2: true,
            antialias: false, // Antialiasing will be handled manually.
            xrCompatible: true
          });
          document.body.appendChild(gl.canvas);

          this.multisampleFramebuffer = gl.createFramebuffer();

          this.renderbufferWidth = 1024;
          this.renderbufferHeight = 1024;
          this.colorRenderbuffer = gl.createRenderbuffer();
          this.target = this.getXRTarget();
          gl.bindRenderbuffer(gl.RENDERBUFFER, this.colorRenderbuffer);
          gl.renderbufferStorageMultisample(gl.RENDERBUFFER, 4, gl.RGBA8, this.renderbufferWidth, this.renderbufferHeight);

          this.depthRenderbuffer = gl.createRenderbuffer();
          gl.bindRenderbuffer(gl.RENDERBUFFER, this.depthRenderbuffer);
          gl.renderbufferStorageMultisample(gl.RENDERBUFFER, 4, gl.DEPTH_COMPONENT16, this.renderbufferWidth, this.renderbufferHeight);

          gl.bindFramebuffer(gl.FRAMEBUFFER, this.multisampleFramebuffer);
          gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, this.colorRenderbuffer);
          gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.depthRenderbuffer);

          this.MRTR = gl.getExtension('WEBGL_multisampled_render_to_texture');
          if (this.MRTR !== null) {
            document.getElementById("useMultisampleTexture").disabled = false;
            document.getElementById("useFoveatedTexture").disabled = false;
          }

          return gl;
        }

        onCreateXRLayer(session) {
          // Don't need antialiasing or depth, since we're handling that in our own framebuffer.
          return new XRWebGLLayer(session, this.gl, { antialias: false, depth: false });
        }

        onXRFrame(time, frame, refSpace) {
          let pose = frame.getViewerPose(refSpace);
          this.scene.updateInputSources(frame, refSpace);
          if (!pose || !this.renderer) { return; }

          let gl = this.renderer.gl;
          let session = frame.session;
          // Assumed to be a XRWebGLLayer for now.
          let layer = session.renderState.baseLayer;

          // Rather than binding the layer's framebuffer like usual, bind a framebuffer using a
          // multisampled renderbuffer as the color attachment.
          gl.bindFramebuffer(gl.FRAMEBUFFER, this.multisampleFramebuffer);

          // Check to see if we need to resize the renderbuffer
          if (layer.framebufferWidth != this.renderbufferWidth ||
              layer.framebufferHeight != this.renderbufferHeight ||
              ((this.target !== this.getXRTarget()) && session.isImmersive)) {
            this.renderbufferWidth = layer.framebufferWidth;
            this.renderbufferHeight = layer.framebufferHeight;

            this.target = this.getXRTarget();
            if (this.target === "useRenderbuffer") {
              gl.bindRenderbuffer(gl.RENDERBUFFER, this.colorRenderbuffer);
              gl.renderbufferStorageMultisample(gl.RENDERBUFFER, 4, gl.RGBA8, this.renderbufferWidth, this.renderbufferHeight);

              gl.bindRenderbuffer(gl.RENDERBUFFER, this.depthRenderbuffer);
              gl.renderbufferStorageMultisample(gl.RENDERBUFFER, 4, gl.DEPTH_COMPONENT16, this.renderbufferWidth, this.renderbufferHeight);

              gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, this.colorRenderbuffer);
              gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.depthRenderbuffer);
            } else {
              this.colorTexture = gl.createTexture();
              this.depthTexture = gl.createTexture();

              gl.bindTexture(gl.TEXTURE_2D, this.colorTexture);

              gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.renderbufferWidth, this.renderbufferHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

              if (this.target === "useFoveatedTexture") {
                let xrGLFactory = new XRWebGLBinding(frame.session, gl);
                if (xrGLFactory.foveateBoundTexture) {
                  xrGLFactory.foveateBoundTexture(gl.TEXTURE_2D, 1);
                }
              }

              if (this.target === "useTexture") {
                gl.bindTexture(gl.TEXTURE_2D, this.depthTexture);
                gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT16, this.renderbufferWidth, this.renderbufferHeight, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null);

                gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.colorTexture, 0);
                gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, this.depthTexture, 0);
              } else {
                gl.bindRenderbuffer(gl.RENDERBUFFER, this.depthRenderbuffer);
                gl.renderbufferStorageMultisample(gl.RENDERBUFFER, 4, gl.DEPTH_COMPONENT16, this.renderbufferWidth, this.renderbufferHeight);

                this.MRTR.framebufferTexture2DMultisampleEXT(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.colorTexture, 0, 4);
              }

            }
          }

          gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

          let views = [];
          for (let view of pose.views) {
            views.push(new WebXRView(view, layer));
          }

          this.scene.drawViewArray(views);

          // Now bind the layer's framebuffer and blit the multisampled framebuffer over to it.
          gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this.multisampleFramebuffer);
          gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, layer.framebuffer);

          gl.blitFramebuffer(0, 0, this.renderbufferWidth, this.renderbufferHeight,
                             0, 0, this.renderbufferWidth, this.renderbufferHeight,
                             gl.COLOR_BUFFER_BIT, gl.LINEAR);
        }
      };

      let app = new CustomWebXRSampleApp();
      document.querySelector('header').appendChild(app.xrButton.domElement);

      app.scene.addNode(new Gltf2Node({url: '../media/gltf/space/space.gltf'}));
      app.scene.addNode(new SkyboxNode({url: '../media/textures/milky-way-4k.png'}));

      // Start the XR application.
      app.run();
    </script>
  </body>
</html>
